package com.github.zly2006.icujavasdk;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;

import java.util.Collections;
import java.util.List;

public class Packet {
    @Getter
    @Setter
    String method;
    @Getter
    @Setter
    JSON data;
    @Getter
    @Setter
    List<String> headers;

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder(method);
        for (String header : headers) {
            ret.append(header).append('\n');
        }
        ret.append(data.toJSONString());
        return ret.toString();
    }

    public Packet(String msg) {
        method = msg.substring(0, msg.indexOf('\n'));
        msg = msg.substring(msg.indexOf('\n') + 1);
        Collections.addAll(headers, msg.substring(0, msg.indexOf("\n\n")).split("\n"));
        data = (JSON) JSON.parse(msg.substring(msg.indexOf("\n\n") + 2));
    }
}
